package apresentacaoSwing;

import java.awt.FlowLayout;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import negocio.Proprietario;
import negocio.GerenteProprietario;
import negocio.ImobiliariaException;


public class TelaNovoProprietario {

	private TelaNovoProprietario frmPrincipal;
	private JFrame frmJanela;

	private JTextField tfNome;
	private JTextField tfDataNasc;
	private JTextField tfEndereco;
	private JTextField tfCpf;
	private JTextField tfTelefone;
	private JTextField tfRg;
	private JTextField tfEmail;
	private JTextField tfEstadoCivil;
	private JTextField tfRenda;

	private GerenteProprietario gerProp;
	private Proprietario prop;
	private TelaProprietario telaP;

	public TelaNovoProprietario(TelaProprietario telaP, GerenteProprietario gerProp) {
		this.gerProp = gerProp;
		this.telaP = telaP;
		montaJanela();
	}

	public void mostrar() {
		frmJanela.setVisible(true);
	}

	private void montaJanela() {

		frmJanela = new JFrame(prop == null ? "Adicionar Proprietario" : "Alterar Proprietario");
		frmJanela.setSize(650,400);
		frmJanela.setLocationRelativeTo(null);
		frmJanela.setLayout(new GridLayout(10, 1));
		frmJanela.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frmJanela.setResizable(false);

		JPanel conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Nome: "));
		conteudo.add(tfNome = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Data de Nascimento: "));
		conteudo.add(tfDataNasc = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Endereco: "));
		conteudo.add(tfEndereco = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("CPF: "));
		conteudo.add(tfCpf = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Telefone: "));
		conteudo.add(tfTelefone = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("RG: "));
		conteudo.add(tfRg = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("E-mail: "));
		conteudo.add(tfEmail = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Estado Civil: "));
		conteudo.add(tfEstadoCivil = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.LEFT));
		conteudo.add(new JLabel("Renda: "));
		conteudo.add(tfRenda = new JTextField(25));
		frmJanela.add(conteudo);

		conteudo = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JButton btAdicionar = new JButton(prop == null ? "Adicionar" : "Alterar");
		btAdicionar.addActionListener(new ManipuladorAdicionar());
		conteudo.add(btAdicionar);
		JButton btCancelar = new JButton("Cancelar");
		btCancelar.addActionListener(
				new ActionListener() {

					public void actionPerformed(ActionEvent e) {

						frmJanela.dispose();

					}

				});
		conteudo.add(btCancelar);
		frmJanela.add(conteudo);
	}

	public JFrame getFrmJanela() {
		return frmJanela;
	}
	public void setFrmJanela(JFrame frmJanela) {
		this.frmJanela = frmJanela;
	}

	private class ManipuladorAdicionar implements ActionListener  {

		private GerenteProprietario gerPro;
		public void actionPerformed(ActionEvent e) {

			try {
					Proprietario p = new Proprietario();
							p.setNome(tfNome.getText());
							p.setDataNasc(tfDataNasc.getText());
							p.setEndereco(tfEndereco.getText());
							p.setCpf(tfCpf.getText());
							p.setTelefone(tfTelefone.getText());
							p.setRg(tfRg.getText());
							p.setEmail(tfEmail.getText());
							p.setEstadoCivil(tfEstadoCivil.getText());
							
						gerPro.adicionar(p);



				}

					catch(ImobiliariaException a){
            JOptionPane.showMessageDialog(null, a.getMessage(),
                    "Erro ao incluir novo cliente",
                    JOptionPane.ERROR_MESSAGE);
					}

					catch(NumberFormatException exc) {
				JOptionPane.showMessageDialog(null,
						"Numero ou data invalida!!!",
						"Erro ao adicionar cliente",
						JOptionPane.ERROR_MESSAGE);}
		}
	}
}